/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.applicatioN.controllers;

import com.applicatioN.controllers.exceptions.NonexistentEntityException;
import com.applicatioN.controllers.exceptions.PreexistingEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import com.applicatioN.entities.Ecole;
import com.applicatioN.entities.Etudier;
import com.applicatioN.entities.EtudierPK;
import com.applicatioN.entities.Specialite;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Asus_k53s
 */
public class EtudierJpaController implements Serializable {

    public EtudierJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Etudier etudier) throws PreexistingEntityException, Exception {
        if (etudier.getEtudierPK() == null) {
            etudier.setEtudierPK(new EtudierPK());
        }
        etudier.getEtudierPK().setIdentifiant(etudier.getCandidat().getIdentifiant());
        etudier.getEtudierPK().setNom(etudier.getEcole().getNom());
        etudier.getEtudierPK().setSpecialite(etudier.getSpecialite1().getSpecialite());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Ecole ecole = etudier.getEcole();
            if (ecole != null) {
                ecole = em.getReference(ecole.getClass(), ecole.getNom());
                etudier.setEcole(ecole);
            }
            Specialite specialite1 = etudier.getSpecialite1();
            if (specialite1 != null) {
                specialite1 = em.getReference(specialite1.getClass(), specialite1.getSpecialite());
                etudier.setSpecialite1(specialite1);
            }
            em.persist(etudier);
            if (ecole != null) {
                ecole.getEtudierCollection().add(etudier);
                ecole = em.merge(ecole);
            }
            if (specialite1 != null) {
                specialite1.getEtudierCollection().add(etudier);
                specialite1 = em.merge(specialite1);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findEtudier(etudier.getEtudierPK()) != null) {
                throw new PreexistingEntityException("Etudier " + etudier + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Etudier etudier) throws NonexistentEntityException, Exception {
        etudier.getEtudierPK().setIdentifiant(etudier.getCandidat().getIdentifiant());
        etudier.getEtudierPK().setNom(etudier.getEcole().getNom());
        etudier.getEtudierPK().setSpecialite(etudier.getSpecialite1().getSpecialite());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Etudier persistentEtudier = em.find(Etudier.class, etudier.getEtudierPK());
            Ecole ecoleOld = persistentEtudier.getEcole();
            Ecole ecoleNew = etudier.getEcole();
            Specialite specialite1Old = persistentEtudier.getSpecialite1();
            Specialite specialite1New = etudier.getSpecialite1();
            if (ecoleNew != null) {
                ecoleNew = em.getReference(ecoleNew.getClass(), ecoleNew.getNom());
                etudier.setEcole(ecoleNew);
            }
            if (specialite1New != null) {
                specialite1New = em.getReference(specialite1New.getClass(), specialite1New.getSpecialite());
                etudier.setSpecialite1(specialite1New);
            }
            etudier = em.merge(etudier);
            if (ecoleOld != null && !ecoleOld.equals(ecoleNew)) {
                ecoleOld.getEtudierCollection().remove(etudier);
                ecoleOld = em.merge(ecoleOld);
            }
            if (ecoleNew != null && !ecoleNew.equals(ecoleOld)) {
                ecoleNew.getEtudierCollection().add(etudier);
                ecoleNew = em.merge(ecoleNew);
            }
            if (specialite1Old != null && !specialite1Old.equals(specialite1New)) {
                specialite1Old.getEtudierCollection().remove(etudier);
                specialite1Old = em.merge(specialite1Old);
            }
            if (specialite1New != null && !specialite1New.equals(specialite1Old)) {
                specialite1New.getEtudierCollection().add(etudier);
                specialite1New = em.merge(specialite1New);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                EtudierPK id = etudier.getEtudierPK();
                if (findEtudier(id) == null) {
                    throw new NonexistentEntityException("The etudier with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(EtudierPK id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Etudier etudier;
            try {
                etudier = em.getReference(Etudier.class, id);
                etudier.getEtudierPK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The etudier with id " + id + " no longer exists.", enfe);
            }
            Ecole ecole = etudier.getEcole();
            if (ecole != null) {
                ecole.getEtudierCollection().remove(etudier);
                ecole = em.merge(ecole);
            }
            Specialite specialite1 = etudier.getSpecialite1();
            if (specialite1 != null) {
                specialite1.getEtudierCollection().remove(etudier);
                specialite1 = em.merge(specialite1);
            }
            em.remove(etudier);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Etudier> findEtudierEntities() {
        return findEtudierEntities(true, -1, -1);
    }

    public List<Etudier> findEtudierEntities(int maxResults, int firstResult) {
        return findEtudierEntities(false, maxResults, firstResult);
    }

    private List<Etudier> findEtudierEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Etudier.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Etudier findEtudier(EtudierPK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Etudier.class, id);
        } finally {
            em.close();
        }
    }

    public int getEtudierCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Etudier> rt = cq.from(Etudier.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
